#pragma once
#include <TIME.H>
#include <stdio.h> 
#include <winsock2.h>

typedef void (CALLBACK* DEALPROC)(SOCKET s, int ServerPort, const char *ClientIP);
typedef void (CALLBACK* CLIENTPROC)(SOCKET s);

 class __declspec(dllexport) Socket
{
public:
	Socket(void);
public:
	~Socket(void);

	struct CLIENTPARA
	{
		SOCKET		s;
		CLIENTPROC	lpClientFunc;
	};
	struct SERVERPARA
	{
		SOCKET		s;
		int			port;
		DEALPROC	lpDealFunc;
	};
	struct DEALPARA
	{
		SOCKET		s;
		int			port;
		char		IP[32];
		DEALPROC	lpDealFunc;
	};

	int GetError();
	SOCKET GetSocket();
	int ReadData(char FAR* buf, int len, int timeout);
	int SendData(const char FAR* buf, int len, int timeout);
	BOOL Connection(LPCSTR pstrHost, int nPort);	
	void Close();
    static DWORD CALLBACK ClientProc(LPVOID lpParm);
	static DWORD CALLBACK ServerProc(LPVOID lpParm);
	static DWORD CALLBACK DealProc(LPVOID lpParm);
	BOOL StartClient(LPCSTR pstrHost, int nPort,CLIENTPROC lpClientFunc);
	BOOL CreateServer(int port, int backlog);
	BOOL StopServer();
	BOOL StartServer(DEALPROC lpDealFunc);
protected:
	int     err;
	int     m_nServerPort;
	SOCKET  m_sSocket;
	BOOL	m_bConnected;
	BOOL	m_bNeedClose;
	BOOL	m_bWSAStartup;
	BOOL    m_bListened;
	HANDLE	m_hClientThread;
	HANDLE  m_hServerThread;
};
